package rs.mbs2.epics.server.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.mbs2.epics.server.entity.Album;
import rs.mbs2.epics.server.entity.User;
import rs.mbs2.epics.server.session.dao.localinterface.AlbumDaoLocal;

@SuppressWarnings("serial")
public class PreUploadImageController extends HttpServlet {

	@EJB
	private AlbumDaoLocal albumDao;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		if(user != null) {
			List<Album> albums = albumDao.findAllAlbumsFromUser(user.getId());
			
			List<Album> kopijaListe = new ArrayList<Album>();
			kopijaListe.addAll(albums);
			
			// brisanje favorites albuma iz liste
			int favIndex = 0;			
			for(int i=0; i < kopijaListe.size(); i++)				
				if(kopijaListe.get(i).getTitle().equalsIgnoreCase("favorites")) {
					favIndex = i;
					break;
				}
			kopijaListe.remove(favIndex);
			
			
			request.setAttribute("userAlbums", kopijaListe);
		}
		
		getServletContext().getRequestDispatcher("/uploadPhoto.jsp").forward(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
}
